<?php

namespace app\admin\model;

use app\common\model\TaskItem;
use think\Model;
use traits\model\SoftDelete;

class Tasks extends Model
{

    use SoftDelete;


    // 表名
    protected $table = 'tasks';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'task_items',
        'pick_images_arr',
        'deliver_images_arr',
        'servicing_images_arr',
        'serviced_images_arr',
        'service_start_images_arr',
        'service_cancel_images_arr',
        'return_images_arr',
        'warning_text',
        'status_text',
        'curr_flow_text',
        'take_status_text',
        'pick_status_text',
        'service_status_text',
        'return_status_text',
        'ext_arr',
    ];

    public function setExt($item, $field, $origin = [])
    {
        $newExt = array_merge($origin, $field);

        $itemExt = $item['ext_arr'];

        return array_merge($itemExt, $newExt);
    }

    public function getExtArrAttr($value, $data)
    {
        $ext = (isset($data['ext']) && $data['ext']) ? json_decode($data['ext'], true) : [];
        return $ext;
    }
    
    public function getPickImagesArrAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['pick_images']) ? $data['pick_images'] : '');
        return !$value ? [] : explode(',', $value);
    }

    public function getDeliverImagesArrAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['deliver_images']) ? $data['deliver_images'] : '');
        return !$value ? [] : explode(',', $value);
    }

    public function getServicingImagesArrAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['servicing_images']) ? $data['servicing_images'] : '');
        return !$value ? [] : explode(',', $value);
    }

    public function getServicedImagesArrAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['serviced_images']) ? $data['serviced_images'] : '');
        return !$value ? [] : explode(',', $value);
    }

    public function getServiceStartImagesArrAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['service_start_images']) ? $data['service_start_images'] : '');
        return !$value ? [] : explode(',', $value);
    }

    public function getServiceCancelImagesArrAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['service_cancel_images']) ? $data['service_cancel_images'] : '');
        return !$value ? [] : explode(',', $value);
    }

    public function getReturnImagesArrAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['return_images']) ? $data['return_images'] : '');
        return !$value ? [] : explode(',', $value);
    }
    
    public function getStatusList()
    {
        return ['-3' => __('Status -3'), '-2' => __('Status -2'), '-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6'), '7' => __('Status 7')];
    }

    public function getCurrFlowList()
    {
        return ['take' => __('Curr_flow take'), 'pick' => __('Curr_flow pick'), 'service' => __('Curr_flow service'), 'return' => __('Curr_flow return'), 'finish' => __('Curr_flow finish')];
    }

    public function getTakeStatusList()
    {
        return ['-3' => __('Take_status -3'), '-2' => __('Take_status -2'),'-1' => __('Take_status -1'), '0' => __('Take_status 0'), '1' => __('Take_status 1')];
    }

    public function getPickStatusList()
    {
        return ['0' => __('Pick_status 0'), '1' => __('Pick_status 1'), '2' => __('Pick_status 2')];
    }

    public function getServiceStatusList()
    {
        return ['-1' => __('Service_status -1'),'0' => __('Service_status 0'), '1' => __('Service_status 1')];
    }

    public function getReturnStatusList()
    {
        return ['0' => __('Return_status 0'), '1' => __('Return_status 1')];
    }

    public function getWarningTypeList()
    {
        return ['1' => '一级', '2' => '二级', '3'=> '三级'];
    }

    public function getWarningTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['warning_type']) ? $data['warning_type'] : '');
        $list = $this->getWarningTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


//return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    public function getExtAttr($value)
    {
        $ext = $value ? json_decode($value, true) : [];
        $ext['expired_time_text'] = !isset($ext['expired_time']) || !$ext['expired_time']  ? '' :date("Y-m-d H:i:s", $ext['expired_time']);
        $ext['invalid_time_text'] = !isset($ext['invalid_time']) || !$ext['invalid_time'] ? '' :date("Y-m-d H:i:s", $ext['invalid_time']);
        $ext['take_time_text'] = !isset($ext['take_time']) || !$ext['take_time'] ? '' :date("Y-m-d H:i:s", $ext['take_time']);
        $ext['refuse_time_text'] = !isset($ext['refuse_time']) || !$ext['refuse_time'] ? '' :date("Y-m-d H:i:s", $ext['refuse_time']);
        $ext['pick_time_text'] = !isset($ext['pick_time']) || !$ext['pick_time'] ? '' :date("Y-m-d H:i:s", $ext['pick_time']);
        $ext['deliver_time_text'] = !isset($ext['deliver_time']) || !$ext['deliver_time'] ? '' :date("Y-m-d H:i:s", $ext['deliver_time']);
        $ext['serviced_time_text'] = !isset($ext['serviced_time']) || !$ext['serviced_time'] ? '' :date("Y-m-d H:i:s", $ext['serviced_time']);
        $ext['service_start_time_text'] = !isset($ext['service_start_time']) || !$ext['service_start_time'] ? '' :date("Y-m-d H:i:s", $ext['service_start_time']);
        $ext['service_cancel_time_text'] = !isset($ext['service_cancel_time']) || !$ext['service_cancel_time'] ? '' :date("Y-m-d H:i:s", $ext['service_cancel_time']);
        $ext['return_time_text'] = !isset($ext['return_time']) || !$ext['return_time'] ? '' :date("Y-m-d H:i:s", $ext['return_time']);
        $ext['refuse_pass_time_text'] = !isset($ext['refuse_pass_time']) || !$ext['refuse_pass_time'] ? '' :date("Y-m-d H:i:s", $ext['refuse_pass_time']);
        return $ext;
    }


    public function getCurrFlowTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['curr_flow']) ? $data['curr_flow'] : '');
        $list = $this->getCurrFlowList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTakeStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['take_status']) ? $data['take_status'] : '');
        $list = $this->getTakeStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPickStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pick_status']) ? $data['pick_status'] : '');
        $list = $this->getPickStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getServiceStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['service_status']) ? $data['service_status'] : '');
        $list = $this->getServiceStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getReturnStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['return_status']) ? $data['return_status'] : '');
        $list = $this->getReturnStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    //用户
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    //取件人
    public function pick()
    {
        return $this->hasOne(User::class, 'id', 'pick_uid');
    }

    //保险公司
    public function insuranceCompany()
    {
        return $this->hasOne(InsuranceCompany::class, 'id', 'insurance_company_id');
    }

    //回款方式
    public function return()
    {
        return $this->hasOne(ReturnType::class, 'id', 'return_id');
    }

    //结算
    public function settle()
    {
        return $this->hasOne(TaskSettle::class,'task_id','id');
    }

    //修复件
    public function getTaskItemsAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['id']) ? $data['id'] : '');
        $taskItems = TaskItem::where('task_id', $value)->order('id asc')->select();
        $data = [];
        foreach ($taskItems as $item) {
            $data[] = $item->part_name.' * '.$item->number;
        }
        return $data;
    }


}
